package cn.funeralobjects.ams.service.impl;

import cn.funeralobjects.ams.entity.ERegion;
import cn.funeralobjects.ams.model.Region;
import cn.funeralobjects.ams.repository.BaseAmsRepository;
import cn.funeralobjects.ams.repository.ERegionRepository;
import cn.funeralobjects.ams.service.RegionService;
import cn.funeralobjects.common.service.annotataion.CommonService;
import cn.funeralobjects.common.service.exception.DataConflictException;
import cn.funeralobjects.util.Assert;
import cn.funeralobjects.util.NumberUtils;

import javax.annotation.Resource;

/**
 * @author FuneralObjects
 * Create date: 2020/5/25 5:50 PM
 */
@CommonService
public class RegionServiceImpl implements RegionService, BaseJpaAmsService<Region, ERegion, Integer> {
    @Resource
    private ERegionRepository eRegionRepository;

    @Override
    public BaseAmsRepository<ERegion, Integer> baseAmsRepository() {
        return eRegionRepository;
    }

    @Override
    public void checkConflict(Region region) throws DataConflictException {
        this.mustNotConflictByCode(region.getRegionId());
    }

    @Override
    public ERegion toEntity(Region region) {
        return region == null ? null : new ERegion()
                .setArea(region.getArea())
                .setRegionId(region.getRegionId())
                .setCityName(region.getCityName())
                .setRegionName(region.getRegionName());
    }

    @Override
    public void checkConflict(Region region, Integer withoutId) throws DataConflictException {
        this.mustNotConflictByCode(region.getRegionId(), withoutId);
    }

    @Override
    public void copyToEntity(Region sourceModify, ERegion targetEntity) {
        targetEntity.setRegionName(sourceModify.getRegionName())
                .setRegionId(sourceModify.getRegionId())
                .setRegionId(sourceModify.getRegionId())
                .setCityName(sourceModify.getCityName())
                .setArea(sourceModify.getArea());

    }

    @Override
    public boolean isValid(Integer id) {
        return NumberUtils.notZero(id);
    }

    @Override
    public void validateModify(Region region) {
        Assert.argAssert(region, "region");
    }
}
